/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Manager_role接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： Manager_roleRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using System.Linq;
using System.Transactions;
using System.Data;

namespace NetCoreCMS.Repository.SqlServer
{
    public class Manager_roleRepository:BaseRepository<Manager_role,Int32>, IManager_roleRepository
    {
        public Manager_roleRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }

        public int Add(Manager_role model)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                
                    int id = (int)_dbConnection.Insert(model);
                    for (int i = 0; i < model.manager_role_values.Count; i++)
                    {
                        model.manager_role_values[i].role_id = id;
                        _dbConnection.Insert<Models.Manager_role_value>(model.manager_role_values[i]);
                    }
                    scope.Complete();
                    return id;
                }
            }
        }

        public bool DeleteAll(int id)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
               
                    _dbConnection.DeleteList<Models.Manager>("where role_id=@id", new { id = id });
                    _dbConnection.DeleteList<Models.Manager_role_value>("where role_id=@id", new { id = id });
                    this.Delete(id);
                    scope.Complete();
                    return true;
                }
            }
        }

        public Models.Manager_role GetModel(int id)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                string conditions = "select * from [" + nameof(Manager_role) + "] where id=@id;select * from [" + nameof(Manager_role_value) + "] where role_id=@id";
                using (var result = _dbConnection.QueryMultiple(conditions, new { id = id }))
                {
                    var role = result.ReadFirstOrDefault<Models.Manager_role>();
                    role.manager_role_values = result.Read<Models.Manager_role_value>().ToList();
                    return role;
                }
            }
        }

        public bool UpdateAll(Manager_role model)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                
                    _dbConnection.Update(model);
                    _dbConnection.DeleteList<Manager_role_value>("where role_id=@id", new { id = model.id });
                    for (int i = 0; i < model.manager_role_values.Count; i++)
                    {
                        model.manager_role_values[i].role_id = model.id;
                        _dbConnection.Insert<Models.Manager_role_value>(model.manager_role_values[i]);
                    }
                    scope.Complete();
                    return true;
                }
            }
        }
    }
}